﻿// lindexi
// 10:56

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Threading.Tasks;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The User Control item template is documented at http://go.microsoft.com/fwlink/?LinkId=234236

namespace SplitViewOptions.BusinessControl
{
    public sealed partial class AdUserControl : UserControl
    {
        public AdUserControl()
        {
            this.InitializeComponent();
        }

        public static async Task<AllAdResult> GetAdData()
        {
            var myClient = new HttpClient();
            var myRequest = new HttpRequestMessage(HttpMethod.Get, Api)
            {
                Version = Version.Parse("1.1")
            };
            myRequest.Headers.Add("X-Bmob-Application-Id", App.Applicationid);
            myRequest.Headers.Add("X-Bmob-REST-API-Key", App.Restkeyid);
            Debug.WriteLine(myRequest.Version.ToString());
            var response = await myClient.SendAsync(myRequest);
            string streamcontent = await response.Content.ReadAsStringAsync();
            AllAdResult resualist = null;
            resualist = (AllAdResult) JsonConvert.DeserializeObject(streamcontent, typeof(AllAdResult));
            return resualist;
        }

        private void TextBlock_Tapped(object sender, TappedRoutedEventArgs e)
        {
            Frame rootFrame = Window.Current.Content as Frame;
            var textBlock = sender as TextBlock;
            if (textBlock != null)
            {
                string str = (textBlock.DataContext as AdInfo)?.AdUrl;
                if (string.IsNullOrEmpty(str))
                {
                    return;
                }
                // Do not repeat app initialization when the Window already has content,
                // just ensure that the window is active
                if (rootFrame != null)
                {
                    App.Currenturl = new Uri(str, UriKind.RelativeOrAbsolute);
                    rootFrame.Navigate(typeof(HomePage));
                    Window.Current.Content = rootFrame;
                    App.Iswithoutpicmode = false;
                    App.Isdownloadstirngmode = false;
                    Window.Current.Activate();
                }
            }
        }

        private async void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            AllAdResult resualist = await GetAdData();

            adviewer.ItemsSource = resualist.Results.Reverse<AdInfo>();
        }

        private static string Api = "https://api.bmob.cn/1/classes/ad";
    }

    public class AdInfo
    {
        public string AdType
        {
            get;
            set;
        }

        public string AdUrl
        {
            get;
            set;
        }

        public string AdText
        {
            get;
            set;
        }

        public string AdPlace
        {
            get;
            set;
        }

        public string AdOwnneremail
        {
            get;
            set;
        }

        public string ImageUrl
        {
            get;
            set;
        }

        public int ClickCount
        {
            get;
            set;
        }

        public string ObjectId
        {
            get;
            set;
        }

        public string CreatedAt
        {
            get;
            set;
        }

        public string UpdatedAt
        {
            get;
            set;
        }
    }

    public class AllAdResult
    {
        public List<AdInfo> Results
        {
            get;
            set;
        }
    }
}